package com.oa.modules.approvalsec1.service.impl;

import cn.hutool.core.lang.hash.Hash;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import cn.hutool.http.HttpRequest;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;
import com.oa.modules.approvalsec1.service.SealService;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Hankaicheng
 * @Date: 2022/5/7 13:34
 */
@Service
public class SealServiceImpl implements SealService {

    private final static String APPID = "866718625498288128";

    private final static String APPSECRET = "blMMnsBTggnVWRogRv";

    //    @Value("${api.url2}")
    private static String URL = "https://dsjj.dezhou.gov.cn/dzyz/api/";

    /**
     * 获取token
     *
     * @return
     */
    public static String getToken() {
        Gson gson = new Gson();
        HashMap<String, Object> data = new HashMap<>();
        data.put("app_id", APPID);
        data.put("app_secret", APPSECRET);
        data.put("grant_type", "client_credentials");
        String sign = createSign(data);
        String result = HttpRequest.post(URL + "v1/oauth/token")
                .body(gson.toJson(data))
                .header("Content-Type", "application/json")
                .execute().body();
        Map map = gson.fromJson(result, Map.class);
        if (map.get("return_code").equals("1000")) {
            LinkedTreeMap d = (LinkedTreeMap) map.get("data");
            return (String) d.get("access_token");
        }
        return "";
    }

    public static void main(String[] args) {
        System.out.println(queryUser("1","1"));
    }

    /**
     * 根据请求内容生成sign
     *
     * @return
     */
    public static String createSign(Map<String, Object> data) {
        Gson gson = new Gson();
        String s = gson.toJson(data);
        String dataStr = s.replace(" ", "");
        String digestHex = "";
        try {
            Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
            SecretKeySpec secretKey = new SecretKeySpec(APPSECRET.getBytes("utf-8"), "HmacSHA256");
            sha256_HMAC.init(secretKey);
            byte[] hash = sha256_HMAC.doFinal(dataStr.getBytes("utf-8"));
            String encodeStr = Base64.encodeBase64String(hash);
            Digester md5 = new Digester(DigestAlgorithm.MD5);
             digestHex = md5.digestHex(encodeStr);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return digestHex;
    }

    public static String queryUser(String xinyong, String company) {
        Gson gson = new Gson();
        HashMap<String, Object> data = new HashMap();
        data.put("credit_code", "91371426MA7HD1MT66");
        data.put("user_name","汇馨(山东)户外用品有限公司\t");
        String token = getToken();
        String sign = createSign(data);
        String body = HttpRequest.post(URL + "v1/user/company/query-user")
                .body(gson.toJson(data))
                .header("sign", sign)
                .header("Content-Type", "application/json")
                .header("token", token)
                .execute().body();
        return body;
    }
}
